<?xml version="1.0" encoding="utf-8" ?>
<!--
/*
 * Nix PBEM Game Engine.
 * Copyright (C) 2008, Lauris Bukšis-Haberkorns
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
-->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" omit-xml-declaration="yes" />
  <xsl:param name="tab" />

  <xsl:template match="/">
	<xsl:apply-templates select="/report" />
	<xsl:text>&#xa;</xsl:text>
	<xsl:apply-templates select="/report/stats" />
	<xsl:text>&#xa;</xsl:text>
	<xsl:apply-templates select="/report/diplomacy" />
	<xsl:text>&#xa;</xsl:text>
	<xsl:apply-templates select="/report/events" />
	<xsl:text>&#xa;&#xa;</xsl:text>
	<xsl:apply-templates select="/report/units" />
	<xsl:text>&#xa;</xsl:text>
	<xsl:apply-templates select="/report/locations" />
  </xsl:template>

  <!-- Header -->
  <xsl:template match="/report">
	<xsl:value-of select="@game"/>
	<xsl:text> Report for Turn </xsl:text>
	<xsl:value-of select="@turn"/>
	<xsl:text> for </xsl:text>
	<xsl:value-of select="@factionName"/>
	<xsl:text>[</xsl:text>
	<xsl:value-of select="@factionTag"/>
	<xsl:text>], </xsl:text>
	<xsl:value-of select="@factionMail"/>
	<xsl:text>.</xsl:text>
	<xsl:text>&#xa;</xsl:text>
  </xsl:template>

  <!-- Day events -->
  <xsl:template name="DayEvent">
	<xsl:param name="tabs" />
	<xsl:param name="day" />
	<xsl:for-each select="event">
	  <xsl:value-of select="$tabs" />
	  <xsl:text>Day </xsl:text>
	  <xsl:value-of select="$day"/>
	  <xsl:text> </xsl:text>
	  <xsl:value-of select="text()"/>
	  <xsl:text>&#xa;</xsl:text>
	</xsl:for-each>
  </xsl:template>

  <!-- Item -->
  <xsl:template name="Item">
	<xsl:value-of select="@count"/>
	<xsl:text> </xsl:text>
	<xsl:value-of select="@name"/>
	<xsl:text>[</xsl:text>
	<xsl:value-of select="@tag"/>
	<xsl:text>]</xsl:text>
	<xsl:if test="@equipped &gt; 0">
	  <xsl:text> (</xsl:text>
		<xsl:value-of select="@equipped"/>
	  <xsl:text> equipped)</xsl:text>
	</xsl:if>
  </xsl:template>

  <!-- Skill -->
  <xsl:template name="Skill">
	<xsl:value-of select="@level"/>
	<xsl:choose>
	  <xsl:when test="@level mod 10 = 1">
		<xsl:text>st</xsl:text>
	  </xsl:when>
	  <xsl:when test="@level mod 10 = 2">
		<xsl:text>nd</xsl:text>
	  </xsl:when>
	  <xsl:when test="@level mod 10 = 3">
		<xsl:text>rd</xsl:text>
	  </xsl:when>
	  <xsl:otherwise>
		<xsl:text>th</xsl:text>
	  </xsl:otherwise>
	</xsl:choose>
	<xsl:text> </xsl:text>
	<xsl:value-of select="@name"/>
	<xsl:text>[</xsl:text>
	<xsl:value-of select="@tag"/>
	<xsl:text>]</xsl:text>
	<xsl:if test="@experience &gt; 0">
	  <xsl:text> (</xsl:text>
	  <xsl:value-of select="@experience"/>
	  <xsl:text>)</xsl:text>
	</xsl:if>
  </xsl:template>

  <xsl:template name="Units">
	<xsl:param name="tabs" />
	<xsl:param name="show_location" />
	<xsl:for-each select="followers/*|units/*">
	  <xsl:if test="name() = 'ownunit'">
		<xsl:variable name="unit_tag" select="@tag" />
		<xsl:for-each select="/report/units/unit[@tag=$unit_tag]">
		  <xsl:call-template name="Unit">
			<xsl:with-param name="tabs" select="$tabs" />
			<xsl:with-param name="prefix" select="'* '" />
			<xsl:with-param name="show_location" select="$show_location" />
		  </xsl:call-template>
		</xsl:for-each>
	  </xsl:if>
	  <xsl:if test="name() = 'unit'">
		<xsl:call-template name="Unit">
		  <xsl:with-param name="tabs" select="$tabs" />
		  <xsl:with-param name="prefix" select="'- '" />
		  <xsl:with-param name="show_location" select="show_location" />
		</xsl:call-template>
	  </xsl:if>
	  <xsl:call-template name="Units">
		<xsl:with-param name="tabs" select="concat($tabs, $tab)" />
		<xsl:with-param name="show_location" select="$show_location" />
	  </xsl:call-template>
	</xsl:for-each>
  </xsl:template>

  <!-- Unit -->
  <xsl:template name="Unit">
	<xsl:param name="tabs" />
	<xsl:param name="prefix" />
  	<xsl:param name="show_location" />

	<xsl:value-of select="$tabs" />
	<xsl:value-of select="$prefix" />
	<xsl:value-of select="@name" />
	<xsl:text>[</xsl:text>
	<xsl:value-of select="@tag" />
	<xsl:text>]</xsl:text>
    <xsl:if test="string-length(@factionTag) &gt; 0">
      <xsl:text> of </xsl:text>
      <xsl:value-of select="@factionName" />
      <xsl:text>[</xsl:text>
      <xsl:value-of select="@factionTag" />
      <xsl:text>]</xsl:text>
    </xsl:if>
  	<xsl:if test="$show_location and string-length(@location) &gt; 0">
      <xsl:text> at </xsl:text>
      <xsl:variable name="curloc" select="@location" />
      <xsl:value-of select="/report/locations/location[@tag=$curloc]/@name" />
      <xsl:text> </xsl:text>
      <xsl:value-of select="/report/locations/location[@tag=$curloc]/@terrain" />
      <xsl:text>[</xsl:text>
      <xsl:value-of select="$curloc" />
      <xsl:text>]</xsl:text>
    </xsl:if>
	<xsl:text>. </xsl:text>
	<xsl:value-of select="race/@figures" />
	<xsl:text> </xsl:text>
	<xsl:value-of select="race/@name" />
	<xsl:text>[</xsl:text>
	<xsl:value-of select="race/@tag" />
	<xsl:text>]</xsl:text>
	<xsl:if test="count(items/item) &gt; 0">
	  <xsl:text> has:</xsl:text>
	  <xsl:for-each select="items/item">
		<xsl:if test="position() &gt; 1">
		  <xsl:text>,</xsl:text>
		</xsl:if>
		<xsl:text> </xsl:text>
		<xsl:call-template name="Item" />
	  </xsl:for-each>
	</xsl:if>
    <xsl:text>.</xsl:text>
	<xsl:if test="weight">
      <xsl:text> Weight: </xsl:text>
      <xsl:value-of select="weight/@unit" />
      <xsl:if test="weight/@stack">
        <xsl:text>(</xsl:text>
        <xsl:value-of select="weight/@stack" />
        <xsl:text>)</xsl:text>
      </xsl:if>
      <xsl:text>.</xsl:text>
	</xsl:if>
	<xsl:if test="count(capacities/capacity) &gt; 0">
	  <xsl:text> Capacity:</xsl:text>
	  <xsl:for-each select="capacities/capacity">
        <xsl:sort select="@type"/>
		<xsl:if test="position() &gt; 1">
		  <xsl:text>,</xsl:text>
		</xsl:if>
		<xsl:text> </xsl:text>
		<xsl:if test="position() &gt; 1">
          <xsl:value-of select="@name" />
          <xsl:text> </xsl:text>
		</xsl:if>
        <xsl:value-of select="@unit" />
		<xsl:if test="@stack">
		  <xsl:text>(</xsl:text>
          <xsl:value-of select="@stack" />
		  <xsl:text>)</xsl:text>
		</xsl:if>
	  </xsl:for-each>
      <xsl:text>.</xsl:text>
	</xsl:if>
	<xsl:if test="count(skills/skill) &gt; 0">
	  <xsl:text> Skills:</xsl:text>
	  <xsl:for-each select="skills/skill">
		<xsl:if test="position() &gt; 1">
		  <xsl:text>,</xsl:text>
		</xsl:if>
		<xsl:text> </xsl:text>
		<xsl:call-template name="Skill" />
	  </xsl:for-each>
      <xsl:text>.</xsl:text>
	</xsl:if>
	<xsl:if test="count(maylearn/skill) &gt; 0">
	  <xsl:text> May learn:</xsl:text>
	  <xsl:for-each select="maylearn/skill">
		<xsl:if test="position() &gt; 1">
		  <xsl:text>,</xsl:text>
		</xsl:if>
		<xsl:text> </xsl:text>
		<xsl:value-of select="@name" />
		<xsl:text>[</xsl:text>
		<xsl:value-of select="@tag" />
		<xsl:text>]</xsl:text>
	  </xsl:for-each>
      <xsl:text>.</xsl:text>
	</xsl:if>
  	<xsl:if test="count(stats/*) &gt; 0">
      <xsl:text> Stats:</xsl:text>
      <xsl:for-each select="stats/*">
        <xsl:if test="position() &gt; 1">
          <xsl:text>,</xsl:text>
        </xsl:if>
        <xsl:text> </xsl:text>
        <xsl:value-of select="name()" />
        <xsl:text>: </xsl:text>
        <xsl:value-of select="text()" />
      </xsl:for-each>
      <xsl:text>.</xsl:text>
    </xsl:if>
  	<xsl:if test="stats/@upkeep &gt; 0">
      <xsl:text> Upkeep: </xsl:text>
      <xsl:value-of select="stats/@upkeep" />
      <xsl:text> coins.</xsl:text>
    </xsl:if>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

  <!-- Faction stats -->
  <xsl:template match="/report/stats">
	<xsl:text>Faction funds:&#xa;</xsl:text>
	<xsl:value-of select="$tab"/>
	<xsl:choose>
	  <xsl:when test="count(funds/item) &gt; 0">
		<xsl:for-each select="funds/item">
		  <xsl:if test="position() &gt; 1">
			<xsl:text>, </xsl:text>
		  </xsl:if>
		  <xsl:call-template name="Item" />
		</xsl:for-each>
	  </xsl:when>
	  <xsl:otherwise>
		<xsl:text>Empty</xsl:text>
	  </xsl:otherwise>
	</xsl:choose>
	<xsl:text>.&#xa;</xsl:text>
	<xsl:text>Control Points: </xsl:text>
	<xsl:value-of select="@controlPoints"/>
	<xsl:text> of </xsl:text>
	<xsl:value-of select="@maxControlPoints"/>
	<xsl:text>.&#xa;</xsl:text>
  </xsl:template>

  <!-- Faction stances -->
  <xsl:template match="/report/diplomacy">
	<xsl:text>Default attitude is </xsl:text>
	<xsl:value-of select="@default"/>
	<xsl:text>.&#xa;</xsl:text>
	<xsl:for-each select="attitude">
	  <xsl:text>Attitude to </xsl:text>
	  <xsl:value-of select="@factionName"/>
	  <xsl:text>[</xsl:text>
	  <xsl:value-of select="@faction"/>
	  <xsl:text>] is </xsl:text>
	  <xsl:value-of select="@stance"/>
	  <xsl:text>.&#xa;</xsl:text>
	</xsl:for-each>
  </xsl:template>

  <!-- Faction global events -->
  <xsl:template match="/report/events">
	<xsl:text>Faction events:&#xa;</xsl:text>
	<xsl:for-each select="event">
	  <xsl:value-of select="concat($tab, text())"/>
	  <xsl:text>&#xa;</xsl:text>
	</xsl:for-each>
	<xsl:for-each select="day">
	  <xsl:call-template name="DayEvent">
		<xsl:with-param name="tabs" select="$tab" />
		<xsl:with-param name="day" select="@number" />
	  </xsl:call-template>
	</xsl:for-each>
  </xsl:template>

  <!-- Faction units -->
  <xsl:template match="/report/units">
	<xsl:text>Units:&#xa;&#xa;</xsl:text>
	<xsl:for-each select="unit">
	  <xsl:call-template name="Unit">
		<xsl:with-param name="tabs" select="''" />
		<xsl:with-param name="prefix" select="@number" />
		<xsl:with-param name="show_location" select="true()" />
	  </xsl:call-template>
	  <xsl:if test="count(events/day) &gt; 0">
		<xsl:text>&#xa;</xsl:text>
		<xsl:value-of select="$tab" />
		<xsl:text>Events for </xsl:text>
		<xsl:value-of select="@name" />
		<xsl:text>[</xsl:text>
		<xsl:value-of select="@tag" />
		<xsl:text>]:&#xa;</xsl:text>
		<xsl:for-each select="events/day">
		  <xsl:call-template name="DayEvent">
			<xsl:with-param name="tabs" select="concat($tab, $tab)" />
			<xsl:with-param name="day" select="@number" />
		  </xsl:call-template>
		</xsl:for-each>
	  </xsl:if>
	  <xsl:text>&#xa;</xsl:text>
	</xsl:for-each>
  </xsl:template>

  <!-- Locations -->
  <xsl:template match="/report/locations">
	<xsl:text>Locations:&#xa;&#xa;</xsl:text>
	<xsl:for-each select="location">
      <xsl:value-of select="@name" />
      <xsl:text> </xsl:text>
      <xsl:value-of select="@terrain" />
      <xsl:text>[</xsl:text>
      <xsl:value-of select="@tag" />
      <xsl:text>].&#xa;</xsl:text>
      <xsl:value-of select="$tab" />
      <xsl:text>Population: </xsl:text>
      <xsl:value-of select="@population" />
      <xsl:text>. Wages: $</xsl:text>
      <xsl:value-of select="@wages" />
      <xsl:text>, entertaiment: $</xsl:text>
      <xsl:value-of select="@entertaiment" />
      <xsl:text>, taxes: $</xsl:text>
      <xsl:value-of select="@taxes" />
      <xsl:text>.&#xa;</xsl:text>
      <xsl:if test="count(market/selling/item) &gt; 0">
        <xsl:value-of select="$tab" />
        <xsl:text>For sale: </xsl:text>
        <xsl:for-each select="market/selling/item">
          <xsl:if test="position() &gt; 1">
            <xsl:text>, </xsl:text>
          </xsl:if>
          <xsl:value-of select="@count" />
          <xsl:text> </xsl:text>
          <xsl:value-of select="@name" />
          <xsl:text>[</xsl:text>
          <xsl:value-of select="@tag" />
          <xsl:text>] at $</xsl:text>
          <xsl:value-of select="@price" />
        </xsl:for-each>
        <xsl:text>.&#xa;</xsl:text>
      </xsl:if>
      <xsl:if test="count(market/buying/item) &gt; 0">
        <xsl:value-of select="$tab" />
        <xsl:text>Buying: </xsl:text>
        <xsl:for-each select="market/buying/item">
          <xsl:if test="position() &gt; 1">
            <xsl:text>, </xsl:text>
          </xsl:if>
          <xsl:value-of select="@count" />
          <xsl:text> </xsl:text>
          <xsl:value-of select="@name" />
          <xsl:text>[</xsl:text>
          <xsl:value-of select="@tag" />
          <xsl:text>] at $</xsl:text>
          <xsl:value-of select="@price" />
        </xsl:for-each>
        <xsl:text>.&#xa;</xsl:text>
      </xsl:if>
      <xsl:text>&#xa;</xsl:text>
      <!-- Location events -->
      <xsl:value-of select="$tab" />
      <xsl:text>Events during turn:&#xa;</xsl:text>
	  <xsl:if test="count(events/day) &gt; 0">
		<xsl:for-each select="events/day">
		  <xsl:call-template name="DayEvent">
			<xsl:with-param name="tabs" select="concat($tab, $tab)" />
			<xsl:with-param name="day" select="@number" />
		  </xsl:call-template>
		</xsl:for-each>
	  </xsl:if>
	  <xsl:if test="count(events/day) = 0">
        <xsl:value-of select="concat($tab, $tab)" />
        <xsl:text>Nothing notable.&#xa;</xsl:text>
	  </xsl:if>
	  <xsl:text>&#xa;</xsl:text>
	  <xsl:call-template name="Units">
		<xsl:with-param name="tabs" select="$tab" />
		<xsl:with-param name="show_location" select="false()" />
	  </xsl:call-template>
	</xsl:for-each>
  </xsl:template>

</xsl:stylesheet>